|
|
|
|
|
|
|
The second line in this code retrieves the information that the user entered into the txtPrincipal text box, placing the value into the variable cPrincipal. This is done with an assignment statement, much like an assignment statement in algebraa variable appears on the left side of the equal sign, and the value to be placed in that variable appears on the right side. The Val() function is used to convert whatever is between the parentheses to a numeric type for subsequent calculation purposes. The actual value in the text box is retrieved by accessing its Text property (you refer to properties of an object in code using the notation object.property). Think of txtPrincipal.Text as being equivalent to the current value of the Text property of txtPrincipal (recall that the Text property of a TextBox control contains the current contents of the box on the screen). |
|
|
|
|
| |
|
|
|
|
As you enter the remaining code, you'll notice several more comments within the code statements. Again, a comment is a line of code that isn't executed; rather, it's used to explain the purpose or functionality of the executable portions of the code. Comments, which are a form of documentation, help a programmer to quickly understand the purpose of a section of code when she must edit it at some point in the future. It's usually a very good idea to include a lot of comments in your code. You may think you'll remember why you solved a problem a certain way, but when you look at code that you (or someone else) wrote a year earlier, comments will make the code's purpose much clearer. |
|
|
|
|
|
|
|
|
|
|
Enter the remaining code presented in Listing 2.1 in the Code window. The comments should help you understand how the code works. Figure 2.19 shows the completed sub procedure in the Code window. |
|
|
|
|
|
|
|
|
Listing 2.1 Code to Calculate the Monthly Payment |
|
|
|
|
|
|
|
|
'Convert interest rate to its decimal equivalent
' i.e. 12.75 becomes 0.1275
fIntrate = Val(txtIntRate.Text) / 100
'Convert annual interest rate to monthly
' by dividing by 12 (months in a year)
fIntrate = fIntrate / 12
'Convert number of years to number of months
' by multiplying by 12 (months in a year)
nTerm = Val(txtTerm.Text) * 12
'Calculate and display the monthly payment.
' The Format function makes the displayed number look good.
cPayment = cPrincipal * (fIntrate / (1 - (1 + fIntrate) ^ -nTerm))
txtPayment.Text = Format(cPayment, "Fixed") |
|
|
|
|
|
|
|
|
I'll bet you're anxious to see your program in action! Before running it, however, make sure you save your work so far. To run the program, you need to execute Visual Basic's Start command using any of these methods: |
|
|
|
|
|